from fastapi import APIRouter, Request
from fastapi import Depends
from config.get_db import get_db
from module_admin.entity.vo.cms_car_team_vo import CarTeamModel, CarTeamForPage, CarTeamForQuery, CarTeamPageObject
from module_admin.entity.vo.cms_car_vo import CarModel, CarPageObject, CarForQuery, CarModelForPage
from module_admin.service.cms_car_team_service import CmsCarTeamService
from module_admin.dao.dept_dao import *
from module_admin.service.login_service import get_current_user_details
from utils.page_util import get_page_obj
from utils.response_util import *
from utils.log_util import *
from utils.time_format_util import object_format_datetime

carTeamController = APIRouter(dependencies=[Depends(get_current_user_details)])


@carTeamController.post("/record/create", summary="车队管理-新增")
async def create_car(request: Request, query: CarTeamModel, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarTeamService.create_car_team(query_db, query)
        return response_200(data=dept_query_result, message="新增成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carTeamController.get("/record/delete/{id}", summary="车队管理-删除")
async def delete_car(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarTeamService.delete_car_team(query_db, id)
        return response_200(data=dept_query_result, message="删除成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carTeamController.get("/record/get/{id}", summary="车队管理-详情")
async def get_car(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarTeamService.get_car_team_by_id(query_db, id)
        object_format_datetime(dept_query_result)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@carTeamController.post("/record/page", response_model=CarModelForPage, summary="车队管理-分页")
async def page_car(request: Request, car_page_query: CarTeamPageObject, db: Session = Depends(get_db)):
    try:
        page_result = CmsCarTeamService.list(db, car_page_query)
        list_format_datetime(page_result)
        # 分页操作
        page_result_after = get_page_obj(page_result, car_page_query.page_num, car_page_query.page_size)
        # 处理
        return response_200(data=page_result_after, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
